/**
 * Created by wjm on 2020/1/2.
 */

export default function(name) {
  return {
    name: name,
    data() {
      return {
        submit_data: {},

        getBefore: {},
        formLabelWidth: 'auto',
        btnName: '',
        formData: {},
        btnNow: {},
        submitData: {},
        originFormData: {},
        selectionArray: [],

        // 需要给分页组件传的信息
        paginate: {
          count: 0,
          currentPage: 1,
          pagesize: 10,
          is_page: true,
          page_sizes: [10, 30, 60, 90],
          layout: 'total, sizes, prev, pager, next, jumper',
        },

        mainData: [],
        self: this,

        table_arguments: {
          height: '70%',
          row_key: 'id',
          tree_props: {
            children: 'child',
            child: 'child',
            hasChildren: 'child'
          },
          default_expand_all: false,
          expand: false,
          selection: true,
          cell_style: {}
        },
        dialog: {
          formLabelWidth: 'auto',
          dialogFormVisible: false,
          title: '用户'
        },
        startSearchItem: {},
        open_show: false,
        searchData: {},

        contentData: {},
        FIND_IN_SET: []
      }
    },
    computed: {

    },

    activated() {
      //console.log('this.$vnode',this)
      /* if(this.$route.query.delKeepAlive){
         console.log('activated',this.$vnode.parent)
         this.$vnode.parent.componentInstance.cache = {};
         this.$vnode.parent.componentInstance.keys = [];
       } */
    },

    watch: {
      /* $route(to, from) {
        console.log(to)
        this.init()
      },
      token() {

      } */
    },

    methods: {

      search() {
        const self = this;
        self.initMainData(true);
      },

      clearSearch() {
        const self = this;
        for (var i = 0; i < self.fields.length; i++) {
          if (self.fields[i]['header_search']) {
            console.log(self.fields[i]);
            self.fields[i]['header_search']['defaultValue'] = '';
            self.$set(self.fields, i, self.fields[i]);
          };
        };
        self.FIND_IN_SET = [];
        self.getBefore = {};
        self.searchItem = self.$$cloneForm(self.startSearchItem);
        if (self.orderSearchItem && self.orderSearchItem.create_time) {
          delete self.orderSearchItem.create_time
        };
        self.initMainData(true);
      },

      openShow() {
        const self = this;
        self.open_show = !self.open_show;
      },

      beforeSearch(TableName) {
        const self = this;
        if (JSON.stringify(self.getBefore) == "{}" && JSON.stringify(self[TableName]['searchItem']) != "{}") {
          self.getBefore[TableName] = self[TableName];
        } else {
          if (JSON.stringify(self[TableName]['searchItem']) == "{}") {
            delete self.getBefore[TableName];
          } else {
            self.getBefore[TableName] = self[TableName];
          };
        };
        console.log('self.getBefore[TableName]', self.getBefore[TableName], TableName, self[TableName]);
        self.initMainData();
      },


      filtersChange(params) {
        const self = this;
        console.log(params);
        for (var key in params) {
          self.searchItem[key] = params[key][0]
        }
        console.log(self.searchItem)
        self.initMainData();
      },

      async header_search_fieldChange(Object) {
        const self = this;
        for (var i = 0; i < self.fields.length; i++) {
          if (self.fields[i]['header_search'] && Object.field.key == self.fields[i]['key']) {
            self.fields[i]['header_search']['defaultValue'] = Object.value;
            self.$set(self.fields, i, self.fields[i]);
          };
        };
        if (Object.func) {
          var res = await Object.func(Object.value, self);
          if (Object.callbak) {
            Object.callbak(res);
          };
          Object.field.header_search['changeFunc'](Object.value, self);
        } else {
          Object.field.header_search['changeFunc'](Object.value, self);
        };
      },

      pageChange(val) {
        console.log('pageChange', val);
        const self = this;
        self.paginate[val[0]] = val[1];
        self.initMainData();
      },

      onClickBtn(val) {
        const self = this;
        self.submitData = {};
        self.formData = val[1];
        self.btnName = val[0];
        self.formData = val[2].func.formData ? self.$$cloneForm(val[2].func.formData(self)) : {};
        self.originFormData = val[1];
        console.log('self.originFormData-first', self.originFormData);
        self.btnNow = val[2];
        if (!val[2].funcType) {
          self.dialog.dialogFormVisible = true;
        } else if (val[2].funcType == 'func') {
          val[2].func.func(this);
        } else if (val[2].funcType == 'submit') {
          self.submit();
        };
      },

      async dialog_fieldChange(Object) {
        const self = this;
        if (Object.field.changeFunc) {
          var res = await Object.field.changeFunc(Object.value, self);
        } else {
          self.formData[Object.field.key] = Object.value;
          self.submitData[Object.field.key] = Object.value;
        };

        if (Object.func) {
          var res = await Object.func(Object.value, self);
          if (Object.callbak) {
            Object.callbak(res);
          };
        };
        if (Object.field && Object.field.dialog && Object.field.dialog['changeFunc']) {
          Object.field.dialog['changeFunc'](Object.value, self);
        };
        console.log('submitData', self.submitData);
      },

      async submit() {
        const self = this;

        var status = 0;
        self.fields.forEach((item, index) => {
          if (status > 0) return;
          if (item.checkKey && (!self.submitData[item.key] || JSON.stringify(self.submitData[item.key]) == '[]' ||
              JSON.stringify(self.submitData[item.key]) == '{}' || self.submitData[item.key] == '')) {
            status = 1;
          }
          if (item.checkReg) {
            // console.log('reg',self.$$checkReg(item.checkReg,self.submitData[item.key]))
            status = self.$$checkReg(item.checkReg, self.submitData[item.key]) ? 0 : 2
          }
        })
        if (status > 0 && self.btnNow.check) {
          self.$message({
            type: 'warning',
            message: status == 1 ? '请完善必填信息' : '填写信息有误，请检查'
          });
          return;
        }

        this.$confirm('是否确定此操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          var postData = self.$$cloneForm(self.btnNow.func.postData(self));
          if (!postData) {

            return;
          };
          console.log('postData', postData)

          var res = await self['$$' + self.btnNow.func.apiName(self)]({
            data: postData
          });
          if (res) {
            if (self.$$sCallBack(res)) {
              self.dialog.dialogFormVisible = false;
              if (self.btnNow.func.callback) {
                self.btnNow.func.callback(self);
              } else {
                self.initMainData();
              };
            };
          };
        }).catch((e) => {
          console.log(e)

        });
      },

      onSelectionChange(val) {
        const self = this;
        self.selectionArray = self.$$cloneForm(val);
        console.log('self.selectionArray', self.selectionArray);
      },


      //订单退款
      async refundOrder(item) {
        const self = this;
        const postData = {};
        postData.order_no = item.order_no;
        postData.token = self.$store.getters.getToken;
        postData.needBack = true;

        this.$confirm('是否确定此操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          var res = await self.$$api_refundOrder({
            data: postData
          });
          if (res) {
            if (self.$$sCallBack(res)) {
              self.dialog.dialogFormVisible = false;
              if (self.btnNow.func.callback) {
                self.btnNow.func.callback(self);
              } else {
                self.initMainData(true);
              };
            };
          };
        }).catch((e) => {
          console.log(e)
          self.$message({
            type: 'info',
            message: '操作失败'
          });
        });

      },


    },

  }
}